/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * UserPagePanel.java
 *
 * Created on Mar 30, 2011, 8:09:47 PM
 */

package org.libarae.client;

import java.net.SocketTimeoutException;
import java.util.Vector;
import javax.swing.JOptionPane;
import org.libarae.communication.ClientInterface;
import org.libarae.communication.common.Book;
import org.libarae.communication.request.ProfileTabRequest;
import org.libarae.communication.response.ProfileTabResponse;
import org.libarae.communication.response.Response;

/**
 *
 * @author Casey, Ben
 */
@SuppressWarnings("serial")
public class UserPagePanel extends javax.swing.JPanel {

    private Vector<Book> mUserBooks;
    private Vector<Book> mCheckedOutBooks;

    private MainScreen mMainScreen;

    /** Creates new form UserPagePanel */
    public UserPagePanel() {
        initComponents();
    }

    public UserPagePanel(MainScreen mMainScreen)
    {
        this.mMainScreen = mMainScreen;
        
        initComponents();
    }

    public void refreshLists()
    {
        ProfileTabRequest r = new ProfileTabRequest();

        Response response = null;

        try {
            response = ClientInterface.sendRequest(r);
        }
        catch(SocketTimeoutException e)
        {
            JOptionPane.showMessageDialog(null, "Request timed out. Please try again.");
            return;
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }

        if(response == null)
        {
            System.err.println("Error sending to or recieveing from server");
        }

        if(response.getStatus() == Response.STATUS_GOOD)
        {
            ProfileTabResponse ptr = (ProfileTabResponse)response;

            mUserBooks = ptr.getUserBookList();
            mCheckedOutBooks = ptr.getCheckedOutBookList();

            lblNewMessages.setText("You have " + ptr.getUnreadMessagesCount() + " messages.");
        }

        //TODO: Handle failure

        blpUserBooks.setBooks(mUserBooks);
        blpCheckedOut.setBooks(mCheckedOutBooks);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        lblUserBooks = new javax.swing.JLabel();
        lblCheckedOut = new javax.swing.JLabel();
        scrCheckedOut = new javax.swing.JScrollPane();
        blpCheckedOut = new org.libarae.client.BookListPanel(mCheckedOutBooks, mMainScreen);
        scrUserBooks = new javax.swing.JScrollPane();
        blpUserBooks = new org.libarae.client.BookListPanel(mUserBooks, mMainScreen);
        btnAddBook = new javax.swing.JButton();
        lblNewMessages = new javax.swing.JLabel();

        lblUserBooks.setText("Your Books");

        lblCheckedOut.setText("Checked Out");

        scrCheckedOut.setViewportView(blpCheckedOut);

        scrUserBooks.setViewportView(blpUserBooks);

        btnAddBook.setText("Add Book");
        btnAddBook.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAddBookActionPerformed(evt);
            }
        });

        lblNewMessages.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        lblNewMessages.setText("You have %n messages.");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(10, 10, 10)
                        .addComponent(btnAddBook)
                        .addContainerGap())
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(lblUserBooks)
                        .addGap(337, 337, 337))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(scrUserBooks, javax.swing.GroupLayout.DEFAULT_SIZE, 380, Short.MAX_VALUE)
                        .addContainerGap())
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(lblCheckedOut)
                                .addGap(318, 318, 318))
                            .addComponent(scrCheckedOut, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 380, Short.MAX_VALUE))
                        .addContainerGap())
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(lblNewMessages)
                        .addContainerGap(255, Short.MAX_VALUE))))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(21, 21, 21)
                .addComponent(lblNewMessages)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(lblUserBooks)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(scrUserBooks, javax.swing.GroupLayout.DEFAULT_SIZE, 98, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnAddBook)
                .addGap(18, 18, 18)
                .addComponent(lblCheckedOut)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(scrCheckedOut, javax.swing.GroupLayout.DEFAULT_SIZE, 82, Short.MAX_VALUE)
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

    private void btnAddBookActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAddBookActionPerformed
        AddEditBookDialog d = new AddEditBookDialog(AddEditBookDialog.ADD_BOOK, mMainScreen);

        //this.getTopLevelAncestor().setEnabled(false);
        d.setVisible(true);
    }//GEN-LAST:event_btnAddBookActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private org.libarae.client.BookListPanel blpCheckedOut;
    private org.libarae.client.BookListPanel blpUserBooks;
    private javax.swing.JButton btnAddBook;
    private javax.swing.JLabel lblCheckedOut;
    private javax.swing.JLabel lblNewMessages;
    private javax.swing.JLabel lblUserBooks;
    private javax.swing.JScrollPane scrCheckedOut;
    private javax.swing.JScrollPane scrUserBooks;
    // End of variables declaration//GEN-END:variables

}
